
<%@page import="org.ow2.bonita.facade.uuid.ActivityInstanceUUID"%><%@ page import="javax.security.auth.login.LoginContext"%>

<%@ page import="org.ow2.bonita.facade.QueryRuntimeAPI"%>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessInstanceUUID"%>
<%@ page import="org.ow2.bonita.util.AccessorUtil"%>
<%@ page import="org.ow2.bonita.util.SimpleCallbackHandler"%>
<%@page import="org.ow2.bonita.facade.runtime.TaskInstance"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.Map"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.ActivityDefinition"%>
<%@page import="org.ow2.bonita.facade.QueryDefinitionAPI"%>
<%@page import="java.util.Set"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.DataFieldDefinition"%>
<%@page import="java.util.HashMap"%>
<%@page import="org.ow2.bonita.facade.runtime.ActivityState"%>

<%
final Object isAdminObject = request.getSession().getAttribute("isadmin");
final boolean isAdmin = isAdminObject != null && ((Boolean) isAdminObject).booleanValue();

final String taskUUID = request.getParameter("taskUUID");
final String msg = request.getParameter("msg");
TaskInstance task = null;
ProcessInstanceUUID instanceUUID = null;
Map<String, String> localDatafieldsTypes = new HashMap<String, String>();
Map<String, String> globalDatafieldsTypes = new HashMap<String, String>();
Map<String, Object> localVariables = Collections.emptyMap();
Map<String, Object> globalVariables = Collections.emptyMap();
String candidates = "";
boolean isReady = false;

try {
  final String username = (String) request.getSession().getAttribute("username");
  final LoginContext loginContext = new LoginContext("BonitaStore", new SimpleCallbackHandler(username, ""));
  loginContext.login();
  
  final ActivityInstanceUUID activityInstanceUUID = new ActivityInstanceUUID(taskUUID);
  instanceUUID = activityInstanceUUID.getProcessInstanceUUID();
  
  final QueryRuntimeAPI queryRuntimeAPI = AccessorUtil.getQueryRuntimeAPI();
  final QueryDefinitionAPI queryDefinitionAPI = AccessorUtil.getQueryDefinitionAPI();
  
  task = queryRuntimeAPI.getTask(activityInstanceUUID);
  isReady = ActivityState.READY.equals(task.getState());
  localVariables = task.getLastKnownVariableValues();
  if (!task.getTaskCandidates().isEmpty()) {
    for (String candidate : task.getTaskCandidates()) {
      candidates += candidate + ",";
    }
    candidates = candidates.substring(0, candidates.length() - 1);
  }
  
  globalVariables = queryRuntimeAPI.getProcessInstanceVariables(instanceUUID);
  
  final ActivityDefinition activityDefinition = queryDefinitionAPI.getProcessActivity(instanceUUID.getProcessDefinitionUUID(), activityInstanceUUID.getActivityName());
  final Set<DataFieldDefinition> localDatafields = activityDefinition.getDataFields();
  for (DataFieldDefinition dataFieldDefinition : localDatafields) {
    localDatafieldsTypes.put(dataFieldDefinition.getName(), dataFieldDefinition.getDataTypeClassName());
  }
  
  final Set<DataFieldDefinition> globalDatafields = queryDefinitionAPI.getProcessDataFields(activityDefinition.getProcessDefinitionUUID());
  for (DataFieldDefinition dataFieldDefinition : globalDatafields) {
    globalDatafieldsTypes.put(dataFieldDefinition.getName(), dataFieldDefinition.getDataTypeClassName());
  }
  
  loginContext.logout();
} catch (Exception e) {
  e.printStackTrace();
  Throwable t = e;
  while (t.getCause() != null) {
    t = t.getCause();
  }
  final String errorMsg = "Error while getting details of task with uuid " + taskUUID + ": " + t.getMessage();
  response.sendRedirect("home.jsp?msg=" + errorMsg);  
}
%>


<jsp:include page="header.html"/>
<a href="home.jsp">Home</a>&nbsp;&nbsp;&nbsp;
<%if (isAdmin) {%>
  <a href="processes.jsp">Manage processes</a>&nbsp;&nbsp;&nbsp;
  <a href="instances.jsp">Manage instances</a>&nbsp;&nbsp;&nbsp;
  <a href="HomeBAM.jsp">Business Activity Monitoring </a>&nbsp;&nbsp;&nbsp;
<%}%>
<br><br>

<%if (msg != null) {%>
  <b><%=msg %></b><br><br>
<%}%>

<TABLE BORDER="1" CELLPADDING="5" WIDTH="600px">
<CAPTION>TASK PROPERTIERS</CAPTION>
<br></br>
<TR>
  <TH>Name</TH>
  <TH>Value</TH>
</TR>

<TR><TD>Name</TD><TD><%=task.getActivityName() %></TD></TR>
<TR><TD>Label</TD><TD><%=task.getActivityLabel() %></TD></TR>
<TR><TD>Dynamic label</TD><TD><%=task.getDynamicLabel() %></TD></TR>
<TR><TD>Description</TD><TD><%=task.getActivityDescription() %></TD></TR>
<TR><TD>Dynamic label</TD><TD><%=task.getDynamicDescription() %></TD></TR>
<TR><TD>Creation date</TD><TD><%=task.getCreatedDate() %></TD></TR>
<TR><TD>Ready date</TD><TD><%=task.getReadyDate() %></TD></TR>
<TR><TD>Started date</TD><TD><%=task.getStartedDate() %></TD></TR>
<TR><TD>Started by</TD><TD><%=task.getStartedBy() %></TD></TR>
<TR><TD>Ended date</TD><TD><%=task.getEndedDate() %></TD></TR>
<TR><TD>Started by</TD><TD><%=task.getEndedBy() %></TD></TR>
<TR><TD>Expected end date</TD><TD><%=task.getExpectedEndDate() %></TD></TR>
<TR><TD>Priority</TD><TD><%=task.getPriority() %></TD></TR>
<TR><TD>State</TD><TD><%=task.getState() %></TD></TR>
<TR><TD>Assigned user</TD><TD><%=task.getTaskUser() %></TD></TR>
<TR><TD>Candidates</TD><TD><%=candidates %></TD></TR>
<TR><TD>Task UUID</TD><TD><%=task.getUUID()%></TD></TR>
<TR><TD>ProcessInstance UUID</TD><TD><%=task.getProcessInstanceUUID()%></TD></TR>
<TR><TD>ProcessDefinition UUID</TD><TD><%=task.getProcessDefinitionUUID()%></TD></TR>
<TR><TD>ActivityDefinition UUID</TD><TD><%=task.getActivityDefinitionUUID()%></TD></TR>

</TABLE>

<br><br>
<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>List of local variables</CAPTION>
<TR>
  <TH>Name</TH>
  <TH>Value</TH>
  <TH>Type</TH>
  <TH>Action</TH>
</TR>

<%
for (Map.Entry<String, Object> variable : localVariables.entrySet()) {
  final String variableName = variable.getKey();
  final Object variableValue = variable.getValue();
  final String type = localDatafieldsTypes.get(variableName); 
  %>
  <TR>
    <TD><%=variableName %></TD>
    <TD><%=variableValue %></TD>
    <TD><%=type %></TD>
    <TD>

  <%if (isReady) {%>
  <form name='updateVariable_local_<%=variableName %>' method='get' action='actions/apiCall.jsp'>
    <input type='text' name='variableValue'>
    <input type='hidden' name='back' value='home'>
    <input type='hidden' name='variableName' value='<%=variableName %>'>
    <input type='hidden' name='taskUUID' value='<%=taskUUID %>'>
    <input type='hidden' name='action' value='setActivityVariable'>
    <input type='submit' name='update' value='update' >
  </form>
  <%}%>
    </TD>
  </TR>
<%}%>
</TABLE>

<br><br>
<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>List of global variables</CAPTION>
<TR>
  <TH>Name</TH>
  <TH>Value</TH>
  <TH>Type</TH>
  <TH>Action</TH>
</TR>
<%
for (Map.Entry<String, Object> variable : globalVariables.entrySet()) {
  final String variableName = variable.getKey();
  final Object variableValue = variable.getValue();
  final String type = globalDatafieldsTypes.get(variableName); 
  %>
  <TR>
    <TD><%=variableName %></TD>
    <TD><%=variableValue %></TD>
    <TD><%=type %></TD>
    <TD>
  <%if (isReady) {%>
  <form name='updateVariable_global_<%=variableName %>' method='get' action='actions/apiCall.jsp'>
    <input type='text' name='variableValue'>
    <input type='hidden' name='back' value='home'>
    <input type='hidden' name='variableName' value='<%=variableName %>'>
    <input type='hidden' name='instanceUUID' value='<%=instanceUUID %>'>
    <input type='hidden' name='action' value='setProcessVariable'>
    <input type='submit' name='update' value='update' >
  </form>
  <%}%>
    </TD>
    </TR>
  
<%}%>
</TABLE>
<br><br>
<%if (isReady) {%>
  <a href="actions/apiCall.jsp?action=executeTask&back=home&taskUUID=<%=taskUUID %>">Execute this task</a>&nbsp;
<%}%>
<br><br>
<jsp:include page="footer.html"/>